<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramName">
                  <a-input v-model="model.paramName" placeholder="请输入参数名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="参数类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramType">
                  <j-dict-select-tag type="list" v-model="model.paramType" @change="changeType" dictCode="machinery_param_type" placeholder="请选择参数类型" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="排序值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramSort">
                  <a-input v-model="model.paramSort" placeholder="请输入排序值"  ></a-input>
                </a-form-model-item>
              </a-col>
               <!-- 动态选项配置区域 -->
              <div v-if="model.paramType == '1'">
                <a-divider orientation="left">选项配置</a-divider>
                <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
                  <a-form-item >
                    <div v-for="(option, index) in options" :key="option.key">
                      <a-input
                        v-model="option.value"
                        placeholder="选项值"
                        style="width: 40%; margin-right: 8px"
                      />
                      <a-icon
                        class="dynamic-delete-button"
                        type="minus-circle-o"
                        @click="removeOption(index)"
                      />
                    </div>
                  </a-form-item>
                  <a-button type="dashed" @click="addOption">
                    <a-icon type="plus" /> 添加选项
                  </a-button>
                </a-form-item>
              </div>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: 'YlMachineryTypeParamModal',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        title:'',
        width:800,
        visible: false,
        disableSubmit: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           paramName: [
              { required: true, message: '请输入参数名称!'},
           ],
           paramType: [
              { required: true, message: '请输入参数类型!'},
           ],
           paramSort: [
              { required: true, message: '请输入排序值!'},
           ],
        },
        url: {
          add: "/machinery/ylMachineryTypeParam/add",
          edit: "/machinery/ylMachineryTypeParam/edit",
          queryById: "/machinery/ylMachineryTypeParam/queryById"
        },
        options: [],
        optionId: 0,
        typeId:''
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      addOption() {
        this.options.push({
          key: this.optionId++,
          value: '',
          label: ''
        });
      },
      removeOption(index) {
        this.options.splice(index, 1);
      },
      add (typeId) {
        this.visible=true
        this.$nextTick(()=>{
          this.$set(this.modelDefault,'typeId',typeId)
          this.edit(this.modelDefault);
        })
      },
      edit (record) {
        this.changeType();
        this.visible=true
        this.typeId = record.typeId
        this.$nextTick(()=>{
          this.model = Object.assign({}, record);
          this.visible = true;
          if(record.selectParam) this.options = JSON.parse(record.selectParam)
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        this.submitForm();
      },
      submitForm () {
          const that = this;
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
              }else{
                httpurl+=this.url.edit;
                 method = 'put';
              }
              this.$set(this.model,'typeId',this.typeId )
              if(this.model.paramType == "1")this.$set(this.model,'selectParam',JSON.stringify(this.options))
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.submitCallback();
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            }

          })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      },
      changeType(){
        this.options = []
      }
    }
  }
</script>
